package com.wine.exam.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wine.common.utils.StringUtils;
import com.wine.exam.domain.Subject;

import com.wine.exam.dto.QuestionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wine.exam.mapper.QuestionMapper;
import com.wine.exam.domain.Question;
import com.wine.exam.service.IQuestionService;

import java.util.Arrays;

/**
 * 题目Service业务层处理
 *
 * @author wine
 * @date 2022-09-02
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;


    /**
     * 查询题目分页：包含条件查询
     *
     * @param questionDto
     * @return
     */
    @Override
    public Page<Question> pageQueryList(QuestionDto questionDto) {

        Page<Question> page = new Page<>();
        page.setCurrent(questionDto.getPageNum());
        page.setSize(questionDto.getPageSize());
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
//      条件。
//        名称：questionTitle
        queryWrapper.like(
                StringUtils.isNotEmpty(questionDto.getQuestionTitle()),
                Question::getQuestionTitle,
                questionDto.getQuestionTitle()
        );
//       类型：questionType
        queryWrapper.eq(
                StringUtils.isNotEmpty(questionDto.getQuestionType()),
                Question::getQuestionType,
                questionDto.getQuestionType()
        );
//       难度：difficult
        queryWrapper.eq(
                StringUtils.isNotEmpty(questionDto.getDifficult()),
                Question::getDifficult,
                questionDto.getDifficult()
        );
//       难度：difficult
        queryWrapper.eq(
                StringUtils.isNotEmpty(questionDto.getQuestionScore()),
                Question::getQuestionScore,
                questionDto.getQuestionScore()
        );
//     学科：SubjectId
        queryWrapper.eq(StringUtils.isNotEmpty(questionDto.getSubjectId()),
                Question::getSubjectId,
                questionDto.getSubjectId()
        );

        Page<Question> list = questionMapper.selectPage(page, queryWrapper);
        return list;

    }

    /**
     * 查询题目
     *
     * @param questionId 题目主键
     * @return 题目
     */
    @Override
    public Question getInfo(Long questionId) {
//        直查
//        return questionMapper.selectById(questionId);
        return questionMapper.selectQuestionLeftSubjectById(questionId);

    }


    /**
     * 新增题目
     *
     * @param question
     * @return 影响行数
     */
    @Override
    public int add(Question question) {

        return questionMapper.insert(question);
    }

    /**
     * 修改题目
     *
     * @param question
     * @return
     */
    @Override
    public int edit(Question question) {

        return questionMapper.updateById(question);
    }


    /**
     * 单独修改题目状态
     *
     * @param questionId
     * @param status
     * @param updateBy
     * @return
     */
    @Override
    public int updateStatus(Long questionId, String status, String updateBy) {
        return questionMapper.updateStatus(questionId, status, updateBy);
    }

    @Override
    public int delByIds(Long[] questionIds) {
        return questionMapper.deleteBatchIds(Arrays.asList(questionIds));
    }


}
